package com.alan.netty.test;

import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author weiBin
 * @date 2018/11/6
 */
public class BeanMain {

    public static void main(String[] args) {
        Bean a1 = new Bean("2,3", "1", "test1");
        Bean a2 = new Bean("4", "2", "test2");
        Bean a3 = new Bean("4", "3", "test3");
        Bean a4 = new Bean("5", "4", "test4");

        Bean a12 = new Bean("7", "6", "test2");
        Bean a13 = new Bean("8", "7", "test3");
        Bean a14 = new Bean("9", "8", "test4");
        List<Bean> list = new ArrayList<>();
        list.add(a1);
        list.add(a2);
        list.add(a3);
        list.add(a4);
        list.add(a12);
        list.add(a13);
        list.add(a14);
        reorder(list);
    }

    private static List<Bean> getParents(Bean bean, List<Bean> list, List<String> childIdList) throws RuntimeException {
        List<Bean> result = new ArrayList<>();
        String ids = bean.getFollowIds();
        if (StringUtils.isEmpty(ids)) {
            result.add(bean);
        } else {
            ids = String.format(",%s,", ids);
            for (Bean temp : list) {
                if (ids.contains(String.format(",%s,", temp.getId()))) {
                    if (childIdList.stream().filter(e -> e.equals(temp.getId())).count() > 0) {
                        throw new RuntimeException("依赖闭环了");
                    }
                    childIdList.add(temp.getId());
                    result.addAll(getParents(temp, list, childIdList));
                    childIdList.remove(temp.getId());
                }
            }
        }
        if (result.size() == 0) {
            result.add(bean);
        }
        return result;
    }

    private static List<List<Bean>> reorder(List<Bean> list) throws RuntimeException {
        if (list == null || list.size() == 0) {
            return null;
        }
        int size = list.size();
        List<List<Bean>> arr = new ArrayList<>();
        while (list.size() > 0) {
            List<String> childIdList = new ArrayList<String>();
            childIdList.add(list.get(0).getId());
            System.out.println("-----------------------------------");
            List<Bean> parents = removeDuplicate(getParents(list.get(0), list, childIdList));
            for (Bean parent : parents) {
                for (Bean bean : list) {
                    if (parent.equals(bean)) {
                        list.remove(bean);
                        break;
                    }
                }
            }
            arr.add(parents);
        }
        while (--size > 0) {
            arr = sort(arr, arr.size() - 1);
        }
        for (int i = 0; i < arr.size(); i++) {
            for (Bean temp : arr.get(i)) {
                System.out.println(String.format("%s: Bean id is %s", i + 1, temp.getId()));
            }
        }
        return arr;
    }

    public static List removeDuplicate(List list) {
        HashSet h = new HashSet(list);
        list.clear();
        list.addAll(h);
        return list;
    }

    public static List<List<Bean>> sort(List<List<Bean>> list, int i) {
        if (list.size() <= i || i < 1) {
            return list;
        } else {
            List<Bean> list1 = list.get(i);
            List<Bean> list2 = list.get(i - 1);
            List<Bean> list3 = new ArrayList<>();
            for (Bean bean : list1) {
                if (list2.stream().filter(e -> bean.getFollowIds().contains(e.getId())).count() == 0) {
                    list3.add(bean);
                }
            }
            list1.removeAll(list3);
            list2.addAll(list3);
            if (list1.size() == 0) {
                list.remove(i);
            } else {
                list.set(i, list1);
            }
            list.set(i - 1, list2);
            return sort(list, --i);
        }
    }
}

